package com.timesheet.gui.efforttracker.controllers;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Calendar;
import java.util.List;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;

import com.timesheet.gui.efforttracker.composite.UtilisationComposite;
import com.timesheet.gui.efforttracker.container.UtilisationModelContainer;
import com.timesheet.gui.efforttracker.providers.UtilisationContentProvier;
import com.timesheet.gui.efforttracker.providers.UtilisationTableLabelProvier;
import com.timesheet.server.common.models.EffortTrackerDTO;
import com.timesheet.server.common.models.view.EffortTrackerViewModel;
import com.timesheet.serverice.UtlisationService;

public class UtilisationController implements PropertyChangeListener {

	UtilisationModelContainer utilisationModelContainer = UtilisationModelContainer
			.getInstance();
	private TableViewer tableViewer;

	public UtilisationController(UtilisationComposite utilisationComposite) {
		init(utilisationComposite);
		utilisationModelContainer.addPropertyChangeListener(this);
		performActions();
	}

	public void init(UtilisationComposite utilisationComposite) {
		tableViewer = utilisationComposite.getTableViewer();
		tableViewer.setContentProvider(new UtilisationContentProvier());
		tableViewer.setLabelProvider(new UtilisationTableLabelProvier());
		tableViewer.setInput(utilisationModelContainer);
	}

	private void performActions() {
		tableRowSelection();
	}

	private void tableRowSelection() {
		tableViewer
				.addSelectionChangedListener(new ISelectionChangedListener() {
					@Override
					public void selectionChanged(SelectionChangedEvent event) {
						ISelection selection = event.getSelection();
						IStructuredSelection structuredSelection = (IStructuredSelection) selection;
						if (structuredSelection.getFirstElement() instanceof EffortTrackerDTO) {
							EffortTrackerDTO dto = (EffortTrackerDTO) structuredSelection
									.getFirstElement();

							Calendar calendar = Calendar.getInstance();
							calendar.set(2014, 11, 22);
							Calendar calendar2 = Calendar.getInstance();
							calendar2.set(2014, 11, 24);
							System.out.println(calendar.getTime());
							System.out.println(calendar2.getTime());

							List<EffortTrackerDTO> dtoKist = new UtlisationService()
									.getDataBetweenDates(
											utilisationModelContainer
													.getFromDate(),
											utilisationModelContainer
													.getToDate(), dto
													.getProjectId());
							utilisationModelContainer
									.setDetailedUtilisationList(dtoKist);
							utilisationModelContainer.refresh("refreshtable");

						}

					}
				});

	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		System.out.println("Fire property change in UtilisationController");
		tableViewer.refresh();

	}

}
